package com.andnnl.template.param;

import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.andnnl.template.ParamUtil;
import com.fasterxml.jackson.databind.JsonNode;

import cn.hutool.core.util.StrUtil;

/**
 * Created by chenss on 2025/7/8.
 */
// 正则表达式类型配置
public class RegexParamConfig extends BaseParamConfig {
    String regexPattern;
    int minLength;
    int maxLength;
    List<String> includes;
    List<String> excludes;

    @Override
    public void validate() {
        if (regexPattern == null || regexPattern.trim().isEmpty())
            throw new IllegalArgumentException("正则表达式缺失: " + name);
        //验证正则表达式
        try {
            Pattern.compile(regexPattern);
        } catch (PatternSyntaxException e) {
            throw new IllegalArgumentException("正则表达式无效: " + name + " regexPattern: "+regexPattern + e.getMessage());
        }
    }

    @Override
    public String generateValue() {
        return ParamUtil.generateByRegex(regexPattern, minLength, maxLength, 3);
    }

    public String getRegexPattern() {
        return regexPattern;
    }

    public void setRegexPattern(String regexPattern) {
        this.regexPattern = regexPattern;
    }

    public int getMinLength() {
        return minLength;
    }

    public void setMinLength(int minLength) {
        this.minLength = minLength;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    @Override
    public void fromJson(JsonNode node) {
        this.regexPattern = node.path("regex").asText(null);
        if (node.has("minLength"))
            this.minLength = node.path("minLength").asInt(5);
        if (node.has("maxLength"))
            this.maxLength = node.path("maxLength").asInt(10);
        if (node.has("includes"))
            this.includes = ParamUtil.parseList(node.path("includes"));
        if (node.has("excludes"))
            this.excludes = ParamUtil.parseList(node.path("excludes"));
    }

    @Override
    public boolean validateValue(String value) throws IllegalArgumentException {
        if (StrUtil.isBlank(value)) {
            throw new IllegalArgumentException("正则参数" + name + "不能为空");
        }
        int length = value.length();
        // 处理最小长度验证（允许minLength=0的场景）
        if ( minLength >0 && length < minLength) {
            throw new IllegalArgumentException("正则参数" + name + "长度必须大于等于" + minLength + "，实际长度：" + length);
        }
        // 仅当maxLength>0时才验证最大长度
        if (maxLength > 0 && length > maxLength) {
            throw new IllegalArgumentException("正则参数" + name + "长度必须小于等于" + maxLength + "，实际长度：" + length);
        }
        if (!Pattern.matches(regexPattern, value)) {
            throw new IllegalArgumentException("参数" + name + "不匹配正则表达式: " + regexPattern + "，实际值：" + value);
        }
        if (includes != null && !includes.isEmpty()) {
            boolean containsInclude = includes.stream().anyMatch(include -> value.contains(include));
            if (!containsInclude) {
                throw new IllegalArgumentException("参数" + name + "必须包含其中一个子字符串: " + String.join(",", includes) + "，实际值：" + value);
            }
        }
        if (excludes != null && !excludes.isEmpty()) {
            boolean containsExclude = excludes.stream().anyMatch(exclude -> value.contains(exclude));
            if (containsExclude) {
                throw new IllegalArgumentException("参数" + name + "不能包含以下子字符串: " + String.join(",", excludes) + "，实际值：" + value);
            }
        }
        return true;
    }
}